package com.guigu.service.impl;

import com.guigu.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Arrays;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {
    private static final String filePath = System.getProperty("user.dir") + "/file/";
    //文件删除
    public String deleteFile(String flag) {
        File file = new File(filePath);
        if (!file.isDirectory()) {
            file.mkdirs();
        }

        List<String> fileNames = Arrays.asList(file.list());
        String avatar = fileNames.stream().filter(name -> name.contains(flag)).findAny().orElse("");

        try {
            if (StringUtils.isNotBlank(avatar)) {
                File deleteFile = new File(filePath + avatar);
                if (deleteFile.exists()) {
                    deleteFile.delete();
                    return "文件删除成功";
                } else {
                    return "文件不存在";
                }
            } else {
                return "找不到符合条件的文件";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "文件删除失败";
        }
    }
}
